﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;

namespace RedmineHelper
{
    public static class AccountHelper
    {
        public static IList<account> LoadAccounts()
        {
            string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "accounts.json");
            FileInfo fi = new FileInfo(file);
            if (fi.Exists)
            {
                StreamReader sr = new StreamReader(file);
                string json = sr.ReadToEnd();
                sr.Close();
                if (!string.IsNullOrEmpty(json))
                    return JsonConvert.DeserializeObject<IList<account>>(json);
                else
                    return new List<account>();
            }
            else
            {
                return new List<account>();
            }
        }

        public static void SaveAccounts(IList<account> accounts)
        {
            string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "accounts.json");
            StreamWriter sw = new StreamWriter(file, false);
            sw.Write(JsonConvert.SerializeObject(accounts));
            sw.Close();
        }
    }
}
